#include <iostream>
#include <string>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
void PrintPending(sigset_t set)
{
    std::cout << "curr process[" << getpid() << "]pending: ";
    for (int i = 31; i >= 1; --i)
        std::cout << sigismember(&set, i);
    std::cout << std::endl;
}
void handler(int signum)
{
    std::cout << signum << "号信号被递达!" << std::endl;
    std::cout << "-------------------------------" << std::endl;
    sigset_t pending;
    sigpending(&pending);
    PrintPending(pending);
    std::cout << "-------------------------------" << std::endl;
}
int main()
{
    signal(2, handler);
    sigset_t block_set, old_set;
    sigemptyset(&block_set);
    sigemptyset(&old_set);
    sigaddset(&block_set, 2);
    sigprocmask(SIG_BLOCK, &block_set, &old_set);
    int cnt = 5;
    while (true)
    {
        sigset_t pending;
        sigpending(&pending);
        PrintPending(pending);
        cnt--;
        if (cnt == 0)
        {
            std::cout << "解除对2号信号的屏蔽!!!" << std::endl;
            sigprocmask(SIG_SETMASK, &old_set, &block_set);
        }
        sleep(1);
    }
}